import json

def eachJsonFile(filepath):
    json_file = open(filepath, 'r', encoding='utf-8')
    js = json.load(json_file)
    OD = {}
    # 遍历 json文件 进行计数
    count = 0
    for _ in js:
        count += 1
    # 创建列表
    for item in js:
        OD[js[item]["tags"]["type"][0]] = []
    # 将相同的键的json写入列表
    for item in js:
        OD[js[item]["tags"]["type"][0]].append({item:js[item]})

    OD_sort = sorted(OD.items(), key=lambda k: k[0])
    # 将排序结果写入json
    res = []
    with open("result2.json", "w", encoding='utf-8') as f:
        for item_od in OD_sort:
            for i in item_od[1]:
                res.append(i)
        json.dump(res, f, indent=2)  # 写为多行
    # 判断数量是否正确
    if len(res) == count:
        print("数量正确")


if __name__ == '__main__':
    # 传入son文件
    # eachJsonFile('./PT澳州随机测试集推图.json')
    eachJsonFile('./PT_EU_随机测试集_20220707.json')
    print('transform success')
